package com.yanqu.road.dao.impl.channelcenter.tonbingchannel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingCampMatchData;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingCommanderData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelTonBingCampMatchDataDaoImpl extends TempDataObjectDao<ChannelTonBingCampMatchData> {

    public static String getChannelTonBingCampDataSql = "select * from `t_n_tonbing_camp_match_data` where `activityId`=?";

    public Map<Integer,Map<Integer, ChannelTonBingCampMatchData>> getChannelTonBingCampMatchDataMap(int activityId) {
        Map<Integer,Map<Integer, ChannelTonBingCampMatchData>> campDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getChannelTonBingCampDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingCampMatchData campData = parse(rs);
                if(!campDataMap.containsKey(campData.getMatchId())){
                    campDataMap.put(campData.getMatchId(),new ConcurrentHashMap<>());
                }
                campDataMap.get(campData.getMatchId()).put(campData.getCampId(), campData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return campDataMap;
    }

    public static String updateChannelTonBingCampDataSql = "update `t_n_tonbing_camp_match_data` set " +
            "collectionData=?,commanderData=? where activityId=? AND campId=? AND matchId=?";

    @Override
    protected boolean updateData(ChannelTonBingCampMatchData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCollectionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCommanderData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchId()));
        if (getDb().executeNoneQuery(updateChannelTonBingCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelTonBingCampDataSql = "insert into `t_n_tonbing_camp_match_data`" +
            "(activityId,campId,collectionData,commanderData,matchId) values (?,?,?,?,?)";

    @Override
    protected boolean addData(ChannelTonBingCampMatchData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCollectionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCommanderData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchId()));
        if (getDb().executeNoneQuery(insertChannelTonBingCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ChannelTonBingCampMatchData parseData(ResultSet rs) throws SQLException {
        ChannelTonBingCampMatchData data = new ChannelTonBingCampMatchData();
        data.setActivityId(rs.getInt("activityId"));
        data.setCampId(rs.getInt("campId"));
        data.setCollectionMap(JSON.parseObject(rs.getString("collectionData"), new TypeReference<ConcurrentHashMap<Long, ConcurrentHashMap<Integer, Long>>>() {
        }));
        data.setCommanderData(JSON.parseObject(rs.getString("commanderData"), new TypeReference<ChannelTonBingCommanderData>() {
        }));
        data.setMatchId(rs.getInt("matchId"));
        return data;
    }

}
